<?php

namespace App\Listeners;

use App\Constant\QueueKey;
use App\Events\UploadIdCardEvent;
use App\Facades\SamLogFacade;
use App\Library\Upload\UploadIdCard;
use App\Models\User;
use App\Models\UserCertOcr;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Log;

class UploadIdCardEventListener implements ShouldQueue
{
    use InteractsWithQueue;

    public $queue = QueueKey::QUEUE_ID_CARD_UPLOAD;

    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Description:
     * Author: Sam
     * DateTime: 2019-03-20 15:02
     * @param UploadIdCardEvent $event
     * @return bool
     * @throws \App\Exceptions\GeneralException
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function handle(UploadIdCardEvent $event)
    {
        /**
         * @var $user User
         */
        $user = $event->user;
        $ocr = UserCertOcr::ByUserId($user->id)->where('is_valid', 1)->get();
        $upload_sign = false;
        foreach ($ocr as $k =>$v){
            if(!empty($v->upload)){
                $upload_sign = true;
                break;
            }
            if(!empty($v->hand_cert)){
                $current = $v;
                break;
            }else{
                $current = null;
            }
        }
        if($upload_sign){
            SamLogFacade::debug('该身份证信息已上传');
            return false;
        }
        $ocr = $current;
        if(empty($ocr)){
            SamLogFacade::debug('用户身份证信息不全');
            return false;
        }
        $upload_arr = ['front', 'back', 'hand'];
        $uploaded = explode(',', $ocr->upload);
        $uploaded = array_filter($uploaded);
        $diff = array_diff($upload_arr, $uploaded);
        SamLogFacade::debug("查找用户【{$user->id}】未上传的文件：".json_encode($diff));
        if (count($diff) > 0) {
            if(!empty($user->nfsp_user_id)){
                if (in_array('front', $diff)) {
                    SamLogFacade::debug("用户【{$user->id}】要上传的身份证正面:" . $ocr->id_card_front);
                    $front = UploadIdCard::getImageInfo($ocr->id_card_front);
                    $front_upload = UploadIdCard::imageUpload($user->nfsp_user_id . '_1.jpg', $front);
                    if ($front_upload === true) {
                        $uploaded[] = 'front';
                    } else {
                        SamLogFacade::debug("用户【{$user->id}】的身份证正面图片" . $ocr->id_card_front . '上传失败');
                    }
                }
                if (in_array('back', $diff)) {
                    SamLogFacade::debug("用户【{$user->id}】要上传的身份证背面:" . $ocr->id_card_back);
                    $back = UploadIdCard::getImageInfo($ocr->id_card_back);
                    $front_upload = UploadIdCard::imageUpload($user->nfsp_user_id . '_2.jpg', $back);
                    if ($front_upload === true) {
                        $uploaded[] = 'back';
                    } else {
                        SamLogFacade::debug("用户【{$user->id}】的身份证背面图片" . $ocr->id_card_back . '上传失败');
                    }
                }
                if (in_array('back', $diff)) {
                    SamLogFacade::debug("用户【{$user->id}】要上传的第三个身份证信息:" . $ocr->hand_cert);
                    $hand = UploadIdCard::getImageInfo($ocr->hand_cert);
                    $front_upload = UploadIdCard::imageUpload($user->nfsp_user_id . '_3.jpg', $hand);
                    if ($front_upload === true) {
                        $uploaded[] = 'hand';
                    } else {
                        SamLogFacade::debug("用户【{$user->id}】的身份证信息图片" . $ocr->id_card_back . '上传失败');
                    }
                }
                if (count($uploaded) > 0) {
                    array_unique($uploaded);
                    $ocr->upload = implode(',', $uploaded);
                    $ocr->save();
                }
            }
        }
    }
}
